package cn.mulanbay.api.system.factory;

import cn.mulanbay.api.system.bean.RemoteCreateWxUser;
import cn.mulanbay.api.system.service.RemoteUserService;
import cn.mulanbay.business.bean.LoginUser;
import cn.mulanbay.business.bean.UserLoginInfo;
import cn.mulanbay.common.domain.R;
import cn.mulanbay.persistent.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;

/**
 * 用户服务降级处理
 *
 * @author fenghong
 */
public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserService> {

    private static final Logger log = LoggerFactory.getLogger(RemoteUserFallbackFactory.class);

    @Override
    public RemoteUserService create(Throwable throwable) {

        log.error("用户服务调用失败:{}", throwable);

        return new RemoteUserService() {
            @Override
            public R<User> getUser(Long id) {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            @Override
            public R<User> getUserByUsername(String username) {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            @Override
            public R<Long> getRoleId(Long userId) {
                return R.fail("获取用户角色失败:" + throwable.getMessage());
            }

            @Override
            public R<LoginUser> loadLoginUser(String loginToken) {
                return R.fail("根据最后一次token获取用户失败:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> updateLoginInfo(UserLoginInfo uli) {
                return R.fail("更新用户登录信息异常:" + throwable.getMessage());
            }

            @Override
            public R<Long> createWxUser(RemoteCreateWxUser cw) {
                return R.fail("创建微信用户异常:" + throwable.getMessage());
            }
        };
    }
}
